<template>
  <div>
    <el-dialog
      title="退订处理"
      :visible.sync="value"
      width="800px"
      :before-close="close"
      :destroy-on-close="true"
    >
      <div>
        <el-descriptions title="" :column="2">
          <el-descriptions-item label="退订单号">{{
              detailInfo.orderId
            }}
          </el-descriptions-item>
          <el-descriptions-item label="订单号">{{
              detailInfo.orderId
            }}
          </el-descriptions-item>
          <el-descriptions-item label="房间">{{
              detailInfo.address
            }}
          </el-descriptions-item>
          <el-descriptions-item label="预订房间">{{
              detailInfo.address
            }}
          </el-descriptions-item>
          <el-descriptions-item label="入离">
            {{ detailInfo.order.checkInTime | formatDate('MM-DD') }}至
            {{ detailInfo.order.checkOutTime | formatDate('MM-DD') }}
          </el-descriptions-item>
          <el-descriptions-item label="入户天数">
            {{
              DateDiff(
                detailInfo.order.checkInTime,
                detailInfo.order.checkOutTime
              )
            }}晚
            <!-- {{ detailInfo.orderRefundItem.refundQuantity }}晚 -->
          </el-descriptions-item>
          <el-descriptions-item label="申请来源">
            <!--            <dict-tag-->
            <!--              :options="dict.type.order_source"-->
            <!--              :value="detailInfo.order.orderSource"-->
            <!--            />-->
            <dict-tag
              :options="dict.type.audit_source"
              :value="detailInfo.auditSource"
            />
          </el-descriptions-item>
          <el-descriptions-item label="取消原因">
            {{ detailInfo.orderStateRecord.recordReason }}
          </el-descriptions-item
          >
          <el-descriptions-item label="申请时间">{{
              detailInfo.createTime
            }}
          </el-descriptions-item>
        </el-descriptions>
        <el-tooltip
          class="item"
          effect="dark"
          content="1.退订费按预订时选择的房间进行房费计算。2.已住房费按预订或续租时选择的房间进行房费计算"
          placement="top-start"
        >
          <div>
            <!--            <el-button type="text">费用明细</el-button>-->
            <!--            <i class="el-icon-info"></i>-->
            <el-descriptions title="费用明细">
              <div slot="title">费用明细 <i class="el-icon-info"></i></div>
            </el-descriptions>
          </div>
        </el-tooltip>
        <price-table :data="detailInfo" :remainingSum.sync="remainingSum" :selection.sync="priceTableSelection"
                     :isCancal="true" :isDetail="isDetail"
        />
      </div>
      <el-form
        :model="auditForm"
        :rules="rules"
        ref="auditForm"
        label-width="100px"
        class="demo-ruleForm"
        v-if="detailInfo.auditRecordState == 1"
      >
        <el-form-item label="处理结果" prop="auditRecordState">
          <el-radio-group v-model="auditForm.auditRecordState">
            <el-radio label="2">退订通过</el-radio>
            <el-radio label="3">退订不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="原因"
          prop="remark"
          v-if="auditForm.auditRecordState == 3"
        >
          <el-input type="textarea" v-model="auditForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <el-descriptions title="" :column="4">
<!--        <el-descriptions-item label="总金额">{{-->
<!--            remainingSum-->
<!--          }}-->
<!--        </el-descriptions-item>-->
<!--        <el-descriptions-item label="优惠金额">{{ detailInfo.order.couponPrice }}</el-descriptions-item>-->
<!--        <el-descriptions-item label="退还金额">{{refundSum}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="退还路径">原路返还</el-descriptions-item>-->
        <el-descriptions-item label="总金额">{{
            detailInfo.order.totalRent+detailInfo.order.deposit
          }}
        </el-descriptions-item>
        <el-descriptions-item label="优惠金额">{{detailInfo.order.couponPrice}}</el-descriptions-item>
        <el-descriptions-item label="退还金额">{{refundSum}}</el-descriptions-item>
        <el-descriptions-item label="退还路径">原路返还</el-descriptions-item>
      </el-descriptions>
      <el-descriptions
        title="退订处理"
        :column="2"
        v-if="detailInfo.auditRecordState != 1"
      >
        <el-descriptions-item label="处理结果">{{
            detailInfo.auditRecordState == 2 ? '退订通过' : '退订不通过'
          }}
        </el-descriptions-item>
        <el-descriptions-item label="处理人">{{
            detailInfo.operatorName
          }}
        </el-descriptions-item>
        <el-descriptions-item label="处理时间">{{
            detailInfo.updateTime
          }}
        </el-descriptions-item>
        <el-descriptions-item label="原因" v-if="detailInfo.auditRecordState == 3 ">{{
            detailInfo.remark
          }}
        </el-descriptions-item>
      </el-descriptions>

      <span slot="footer" class="dialog-footer" v-if="detailInfo.auditRecordState == 1">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getcancelReservation } from '@/api/house/condition'
import { cancelList, auditRecordInfo, auditRecord } from '@/api/order/continue'
import { getDayOrWeek, inTime, DateDiff } from '@/utils/common/time'

import priceTable from '@/views/daily-rent/unsubscribe/components/price-table.vue'

export default {
  name: 'checkOutOrder',
  dicts: ['order_source', 'audit_source'],
  components: {
    priceTable
  },
  data() {
    return {
      auditForm: {},
      detailInfo: {},
      priceTabel: [],
      remainingSum: 0,
      selection: [],
      priceTableSelection: [],
      // 表单校验
      rules: {
        auditRecordState: [
          { required: true, message: '选择处理结果', trigger: 'change' }
        ],
        remark: [{ required: true, message: '请填写原因', trigger: 'blur' }]
      }
    }
  },
  props: {
    orderId: {
      type: String,
      default: ''
    },
    recordId: {
      default: ''
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    isDetail() {
      if (this.detailInfo.auditRecordState) {
        if (this.detailInfo.auditRecordState == 2 || this.detailInfo.auditRecordState == 3) {
          return true
        }
      } else {
        return false
      }
      return false
    },
    // 退还金额计算
    refundSum() {
      //获取不需要退订费用id
      let noRefundDetailsIds = []
      this.priceTableSelection.map(item=>{
        if(item.parentKey == 'unsubscribeRefundItem'){
          noRefundDetailsIds.push(item.id);
        }
      })
      console.log('noRefundDetailsIds',noRefundDetailsIds)
      // if(this.detailInfo.orderRefundDetailsList.length === noRefundDetailsIds.length){
      //   return this.remainingSum
      // }else{
      //   return this.remainingSum - this.detailInfo.order.couponPrice
      // }
      console.log('this.detailInfo.order.couponPrice',this.detailInfo.order.couponPrice)
      console.log('this.remainingSum',this.remainingSum)
      if(this.detailInfo.order.couponPrice){
        return this.remainingSum - this.detailInfo.order.couponPrice
      }else{
        return this.remainingSum
      }

    }
  },

  created() {
    this.getInfo()
  },
  mounted() {
    // this.getInfo()
  },
  methods: {
    DateDiff: DateDiff,
    submit() {
      this.$refs['auditForm'].validate((valid) => {
        if (valid) {

          //获取不需要退订费用id
          let noRefundDetailsIds = []
          this.priceTableSelection.map(item => {
            if (item.parentKey == 'unsubscribeRefundItem') {
              noRefundDetailsIds.push(item.id)
            }
          })
          //
          //获取所有费用id
          let ids = this.detailInfo.orderRefundDetailsList.map(item => {
            return item.id
          })
          //获取未选中的费用(未选中即为不扣除的费用,即退订费用)
          let refundDetailsIds = ids.filter(item => {
            return !noRefundDetailsIds.includes(item)
          })
          //
          console.log('ids:所有', ids, 'noRefundDetailsIds:不退', noRefundDetailsIds, 'refundDetailsIds:退', refundDetailsIds)

          let form = { ...this.detailInfo }
          form.refundDetailsIds = noRefundDetailsIds
          form.auditRecordState = this.auditForm.auditRecordState
          form.remark = this.auditForm.remark
          console.log('form', form)
          // return
          auditRecord(form).then((res) => {
            this.$modal.msgSuccess(res.msg || '操作成功')
            this.close()
          })
        }
      })
    },
    checkSelectable(row) {
      if (this.isCheck) {
        return false
      } else {
        return row.chargeType ? false : true
      }

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let totalPrice = 0,
        totalRelet = 0
      //this.ids = selection.map((item) => item.orderId);
      selection.map((item) => {
        if (item.chargeType) {
          totalPrice += item.itemSum
        } else {
          totalRelet += item.rentingExpenses ? item.rentingExpenses : 0
        }
      })
      this.selection = selection
      this.remainingSum = totalPrice - totalRelet
    },
    handleClose() {
    },
    close() {
      this.$emit('input', false)
    },
    getInfo() {
      getcancelReservation(this.recordId).then((res) => {
        this.detailInfo = res.data
      })
    },
    toggleSelection(rows) {
      this.$nextTick(() => {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row, true)
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped></style>
